﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    class CrossArea
    {
        private soDatasets objDSs = null;
        private AxSuperMapLib.AxSuperMap objMap = null;

        public CrossArea(AxSuperMapLib.AxSuperMap objMap, Utility utl)
        {
            utl.InitializeMap(objMap);
            this.objDSs = utl.objSource.Datasets;
            this.objMap = objMap;
        }

        public void BuildCrossAreaForMap()
        {
            AddBufferLayerToMap();
        }

        private void AddBufferLayerToMap()
        {
            soLayers objLayers = null;
            soLayer objLayer200m = null;
            soLayer objLayerSrc = null;
            string strLayerName = string.Empty;

            objLayers = objMap.Layers;
            strLayerName = objDSs[objDSs.Count].Name + "@" + Utility.datasourceAlias;
            objLayer200m = objMap.Layers.AddDataset(objDSs[objDSs.Count], false);
            if (objLayer200m != null)
            {
                objLayerSrc = objLayers["joukoujouR@areamap"];
                if (!objLayers.MoveTo(objLayers.Count, objLayers.FindLayer(objLayerSrc)))
                {
                    throw new Exception("Layer move failed!");
                }

                //objMap.Refresh();
                objMap.SaveMap();
                objMap.Close();
                objMap.Disconnect();

                Marshal.ReleaseComObject(objLayerSrc);
                objLayerSrc = null;
                Marshal.ReleaseComObject(objLayer200m);
                objLayer200m = null;
            }

            Marshal.ReleaseComObject(objLayers);
            objLayers = null;
            //Marshal.ReleaseComObject(objMap);
            //objMap = null;
        }
    }
}
